package com.atguigu.yygh.hosp.testmongodb;

import com.atguigu.yygh.common.R;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Api(tags = "测试MgonoDB")
@RestController
@RequestMapping("/mongo1")
public class TestMongo1 {

    @Autowired
    private MongoTemplate mongoTemplate;

    @ApiOperation("添加")
    @GetMapping("create")
    public R create(){

        User user = new User();
        user.setAge(20);
        user.setName("test");
        user.setEmail("4932200@qq.com");

        User save = mongoTemplate.save(user);
        //自动主键回填
        return R.ok().data("user",save);

    }

    //查询所有
    @ApiOperation("查询所有")
    @GetMapping("findAll")
    public R findAll(){
        List<User> all = mongoTemplate.findAll(User.class);

        return R.ok().data("list",all);
    }

    //根据id查询
    @ApiOperation("根据id查询")
    @GetMapping("findById/{id}")
    public R findById(@PathVariable String id){
        User user = mongoTemplate.findById(id, User.class);

        return R .ok().data("user",user);
    }


    //条件查询
    @ApiOperation("条件查询")
    @GetMapping("findUser")
    public R findUser(){
        Query query = new Query(Criteria.where("name").is("test").and("age").is(20));

        List<User> users = mongoTemplate.find(query, User.class);

        return R.ok().data("list",users);
    }

    //模糊查询
    @ApiOperation("模糊查询")
    @GetMapping("findLike")
    public R findLike(){
        String name = "est";
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        //这里的模糊查询是使用正则的形式去进行模糊查询的
        Query query = new Query(Criteria.where("name").regex(pattern));

        List<User> users = mongoTemplate.find(query, User.class);
        return R.ok().data("users",users);
    }

    //分页查询
    @ApiOperation("模糊查询带分页")
    @GetMapping("findPage")
    public R findUsersPage() {
        //使用模糊查询，定义name
        String name = "est";
        //定义起始页
        int pageNo = 1;
        //定义每页显示的个数
        int pageSize = 10;

        //定义条件对象
        Query query = new Query();
        //使用正则定义模糊查询的条件
        String regex = String.format("%s%s%s", "^.*", name, ".*$");
        Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        //将正则的条件加入到query中
        query.addCriteria(Criteria.where("name").regex(pattern));

        //查询总条数
        int totilCount = (int)mongoTemplate.count(query,User.class);
        //分页查询数据
        List<User> users = mongoTemplate.find(query.skip((pageNo - 1) * pageSize).limit(pageSize), User.class);

        //定义一个map，将数据封装到map中
        Map<String, Object> map = new HashMap<>();
        map.put("totil",totilCount);
        map.put("list",users);

        return R.ok().data("map",map);

    }

    //修改
    @ApiOperation("根据id修改一条数据")
    @GetMapping("update")
    public R updateUser() {
        //修改首先先根据id进行查询，
        User user = mongoTemplate.findById("5ffbfa2ac290f356edf9b5aa", User.class);
        user.setName("zhangsan");
        user.setAge(30);
        //设置条件，根据id进行修改
        Query query = new Query(Criteria.where("_id").is(user.getId()));

        Update update = new Update();
        update.set("name",user.getName());
        update.set("age",user.getAge());
        update.set("email",user.getEmail());

        UpdateResult upsert = mongoTemplate.upsert(query, update, User.class);

        //返回影响的行数
        long modifiedCount = upsert.getModifiedCount();

        System.out.println("modifiedCount = " + modifiedCount);

        return R.ok();
    }

    //删除操作
    @ApiOperation("根据id删除一条数据")
    @GetMapping("delete")
    public R delete() {
        Query query = new Query(Criteria.where("_id").is("5ffbfa2ac290f356edf9b5aa"));

        DeleteResult remove = mongoTemplate.remove(query, User.class);

        long deletedCount = remove.getDeletedCount();

        System.out.println("deletedCount = " + deletedCount);

        return R.ok();
    }
}
